package com.tuling.spring.factory;

import com.tuling.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Proxy;

public class MyFactoryBean implements FactoryBean {

    private Class targetClass;

    private SqlSession sqlSession;

    public MyFactoryBean(Class clazz) {
        this.targetClass = clazz;
    }

    @Autowired
    public void setSqlSession(SqlSessionFactory sqlSessionFactory) {
        //sqlSessionFactory.getConfiguration().addMapper(targetClass);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    @Override
    public Object getObject(){
        return this.sqlSession.getMapper(targetClass);
    }

    @Override
    public Class<?> getObjectType() {
        return targetClass;
    }
}
